/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
//#include "usart.h"
#include "queue.h"
#include <string.h> 
#include "udp_task.h"
#include "tcp_task.h"
#include "MotorsTask.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
	//初始化队列
	osMessageQueueId_t printQueueHandle;
	osMessageQueueId_t tcpQueueHandle;
	osMessageQueueId_t udpTaskHandle;
	osMessageQueueId_t motorQueueHandle;
	//共享TCP连接
	struct netconn *shared_conn;
/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
osThreadId_t tcpClientTaskHandle;
 const osThreadAttr_t tcpClientTask_attributes = {
        .name = "TCP_ClientTask",
        .priority = (osPriority_t) osPriorityHigh,
        .stack_size = 512*4 // 根据需要调整堆栈大小
    };
    	
//osThreadId_t tcpSendTaskHandle;
//	const osThreadAttr_t tcpSendTask_attributes = {
//					.name = "tcpSendTask",
//					.stack_size = 512 * 4,
//					.priority = (osPriority_t) osPriorityNormal,
//			};		
	
osThreadId_t MotorsTaskHandle;
const osThreadAttr_t MotorsTask_attributes = {
  .name = "MotorsTask",
  .stack_size = 512 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};

osMessageQueueId_t udpQueueHandle;
// 定义UDP任务的属性
const osThreadAttr_t udpTask_attributes = {
    .name = "UDP_Task",
    .priority = (osPriority_t) osPriorityAboveNormal,
    .stack_size = 512 * 4 // 根据需要调整堆栈大小
};
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
	tcpQueueHandle = osMessageQueueNew(20, 50, NULL);
	motorQueueHandle = osMessageQueueNew(10, sizeof(Send_Data), NULL);
	udpQueueHandle = osMessageQueueNew(20, sizeof(Send_Data), NULL); // 队列大小和每条消息的大小
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
	udpTaskHandle = osThreadNew(UDPSendTask, NULL, &udpTask_attributes);
	tcpClientTaskHandle = osThreadNew(TCPReceiveTask, NULL, &tcpClientTask_attributes);
//	tcpClientTaskHandle = osThreadNew(TCPSendTask, NULL, &tcpClientTask_attributes);
	MotorsTaskHandle = osThreadNew(MotorsTask, NULL, &MotorsTask_attributes);


  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
		HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_3);
    osDelay(500);
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

